package edu.zut.cs.javaee.dream.teach.web.spring.controller;

import com.alibaba.fastjson.JSON;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import edu.zut.cs.javaee.dream.base.web.spring.controller.GenericController;
import edu.zut.cs.javaee.dream.teach.domain.Student;
import edu.zut.cs.javaee.dream.teach.service.StudentManager;


import java.util.HashMap;
import java.util.Map;


/**
 *  teach-student
 * @author liuxiaoming
 *
 */
@RestController
@RequestMapping(value="/teach/student",produces = { MediaType.APPLICATION_JSON_VALUE })
public class StudentController extends GenericController<Student, Long, StudentManager> {

	public static final Logger logger = LogManager.getLogger(StudentController.class.getName());

	StudentManager studentManager;


	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
		this.manager = this.studentManager;
	}



	@RequestMapping(value="/findAll/{page}/{pageSize}",method = {RequestMethod.GET,RequestMethod.POST})
	public Map<String,Object> findAllStudent(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
		Sort sort = Sort.by(Sort.Direction.DESC,"id");
		Pageable pageable = PageRequest.of(page,pageSize, sort);
		Page<Student> list = studentManager.findAll(pageable);

		HashMap<String,Object> map = new HashMap<>();
		map.put("number",list.getTotalElements());
		map.put("userList",list.getContent());
		logger.info("----------->"+list.getContent());
		return map;
	}

}
